package capricornus.scene.controller
{
	import capricornus.events.ResLoaderEvent;
	import capricornus.net.Path;
	import capricornus.res.Image;
	import capricornus.res.ImageFitType;
	import capricornus.scene.SceneManager;
	import capricornus.scene.preload.ScenePreLoadController;
	import capricornus.scene.preload.ScenePreLoadEvent;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;

	[Event(name="complete", type="flash.events.Event")]
	internal class SceneCellImageController extends EventDispatcher
	{
		internal static const CELL_WIDTH:int = 300;
		internal static const CELL_HEIGHT:int = 300;
		
		//缩略图放大比例
		internal static const SCALE_RATE:Number = 10; 
		
		private var _parent:DisplayObjectContainer;
		private var _rect:Rectangle;
		
		private var _imageLoaded:Boolean = false;
		private var _image:Image;
		private var _hasAddPrevloadEvent:Boolean = false;
		private var _hasDrawed:Boolean = false;
		private var _defaultRect:Rectangle;
		private var _defaultBitmapData:BitmapData;
		
		public function SceneCellImageController( parent:DisplayObjectContainer )
		{
			_parent = parent;
			init();
		}
		
		private function init():void
		{
			_defaultRect = new Rectangle( 0, 0, CELL_WIDTH, CELL_HEIGHT );
			_defaultBitmapData = new BitmapData( CELL_WIDTH, CELL_HEIGHT, true, 0 );
			
			_image = new Image( ImageFitType.EXACT_FIT );
			_image.defaultBitmapData = _defaultBitmapData;
			_image.setSize( CELL_WIDTH, CELL_HEIGHT );
			_image.addEventListener( ResLoaderEvent.COMPLETE, onImageLoaded, false, 0, true );
		}
		
		public function clear():void
		{
			_image.source = null;
		}
		
		/**
		 * 重置发生在地图缩略图加载完成之后 
		 * @param id
		 * @param i
		 * @param j
		 * 
		 */		
		public function reset( id:String, i:int, j:int ):void
		{
			_imageLoaded = false;
			_rect = new Rectangle( i*CELL_WIDTH, j*CELL_HEIGHT, CELL_WIDTH, CELL_HEIGHT );
			_defaultBitmapData.fillRect( _defaultRect, 0x00000000 );
			_hasDrawed = false;
			_hasAddPrevloadEvent = false;

			draw();
			_image.setLocation( _rect.x, _rect.y );
//			_image.source = Path.getSceneCellImagePath( id, i, j );
			_image.source = Path.getSceneCellImagePath( "E_1001A", i, j );
		}
		
		public function viewPortChange( rect:Rectangle ):void
		{
			if( _rect.intersects( rect ) )
			{
				if( _image.parent==null )
				{
					_parent.addChild( _image );
					draw();
				}
			}
			else if( _image.parent != null )
			{
				_parent.removeChild( _image );
			}
		}
		
		private function draw():void
		{
			if( _hasDrawed==false )
			{
				if( !_imageLoaded && _image.parent != null )
				{
					if( preloadController.bmPreview==null )
					{
						if( _hasAddPrevloadEvent==false )
						{
							_hasAddPrevloadEvent = true;
							preloadController.addEventListener( ScenePreLoadEvent.SCENE_PRELOAD_COMPLETE, onLoaded, false, 0, true );
						}
					}
					else
					{
						_hasDrawed = true;
						var bd:BitmapData = preloadController.bmPreview.bitmapData;
						if( bd != null )
						{
							var mt:Matrix = new Matrix( SCALE_RATE, 0, 0, SCALE_RATE );
							mt.tx = -_rect.x / SCALE_RATE;
							mt.ty = -_rect.y / SCALE_RATE;
							_defaultBitmapData.draw( bd, mt );
						}
					}
				}
			}
		}
		
		private var _preloaderController:ScenePreLoadController = null;
		private function get preloadController():ScenePreLoadController
		{
			if( _preloaderController==null )
			{
				_preloaderController = SceneManager.instance.scene.preLoadController
			}
			return _preloaderController;
		}
		
		private function onLoaded( e:ScenePreLoadEvent ):void
		{
			preloadController.removeEventListener( ScenePreLoadEvent.SCENE_PRELOAD_COMPLETE, onLoaded );
			draw();
		}
		
		private function onImageLoaded( e:ResLoaderEvent ):void
		{
			_imageLoaded = true;
			dispatchEvent( new Event( Event.COMPLETE ) );
		}
	}
}